import java.util.Iterator;

import jess.Fact;
import jess.JessException;
import jess.Rete;

public class TrafficControl {
    public static void main(String args[]) {
        Rete rete = new Rete();
        try {
            System.out.println("Input: ");
            TrafficSystem input = new TrafficSystem("trafficConfig.txt");

            rete.executeCommand("(batch rules.clp)");

            rete.executeCommand("(assert (" + input.WNcars + "))");
            rete.executeCommand("(assert (" + input.WWcars + "))");
            rete.executeCommand("(assert (" + input.WScars + "))");
            rete.executeCommand("(assert (" + input.WEcars + "))");

            rete.executeCommand("(assert (" + input.ENcars + "))");
            rete.executeCommand("(assert (" + input.EWcars + "))");
            rete.executeCommand("(assert (" + input.EScars + "))");
            rete.executeCommand("(assert (" + input.EEcars + "))");

            if (input.WEWwalk) {
                rete.executeCommand("(assert (PWEW))");
            }

            if (input.WNSwalk) {
                rete.executeCommand("(assert (PWNS))");
            }

            if (input.EEWwalk) {
                rete.executeCommand("(assert (PEEW))");
            }

            if (input.ENSwalk) {
                rete.executeCommand("(assert (PENS))");
            }

            if (input.emergency) {
                
                rete.executeCommand("(assert (emergency " + input.emergencyArea + "))");
                if (input.emergencyArea.charAt(0) == 'W') {
                    rete.executeCommand("(assert (no-e-emergency))");
                    rete.executeCommand("(assert (w-emergency))");
                } else if (input.emergencyArea.charAt(0) == 'E') {
                    rete.executeCommand("(assert (no-w-emergency))");
                    rete.executeCommand("(assert (e-emergency))");
                } else {
                }

            } else {
                rete.executeCommand("(assert (no-w-emergency))");
                rete.executeCommand("(assert (no-e-emergency))");
            }

            rete.executeCommand("(assert (LW" + input.WLightDirection + "))");
            rete.executeCommand("(assert (LE" + input.ELightDirection + "))");

            if (input.backup) {
                rete.executeCommand("(assert (backup))");
            }

            if (input.emergency) {
                rete.executeCommand("(assert (emergency))");
                rete.executeCommand("(bind ?el \"" + input.emergencyLocation + "\" )");
            }

            System.out.println("\nOutput: ");

            rete.run();

            @SuppressWarnings("unchecked")
            Iterator<Fact> facts = rete.listFacts();

            String west = new String();
            String east = new String();
            boolean westWalk = false;
            boolean eastWalk = false;

            while (facts.hasNext()) {
                Fact fact = facts.next();
                //System.out.println(fact);
                if (fact.toString().contains("west-traffic-light")) {
                    west = fact.toString().replace("MAIN::west-traffic-light ", "").replace("(", "").replace(")", "");
                }

                if (fact.toString().contains("east-traffic-light")) {
                    east = fact.toString().replace("MAIN::east-traffic-light ", "").replace("(", "").replace(")", "");
                }

                if (fact.toString().contains("w-pause-continue")) {
                    westWalk = true;
                }

                if (fact.toString().contains("e-pause-continue")) {
                    eastWalk = true;
                }

            }

            String westString = "West Intersection: ";
            if (westWalk) {
                westString += "Pause for pedestrians between light change. ";
            }

            if (west.equals("normal")) {
                westString += "Continue normal operation";
            } else if (west.equals("change")) {
                westString += "Change light immediately";
            } else if (west.equals("extend")) {
                westString += "Extend light duration";
            } else if (west.equals("shorten")) {
                westString += "Shorten light duration";
            } else if (west.equals("terminate")) {
                westString += "Change light to blinking red";
            }

            String eastString = "East Intersection: ";
            if (eastWalk) {
                eastString += "Pause for pedestrians between light change. ";
            }
            if (east.equals("normal")) {
                eastString += "Continue normal operation";
            } else if (east.equals("change")) {
                eastString += "Change light immediately";
            } else if (east.equals("extend")) {
                eastString += "Extend light duration";
            } else if (east.equals("shorten")) {
                eastString += "Shorten light duration";
            } else if (east.equals("terminate")) {
                eastString += "Change light to blinking red";
            }

            System.out.println(westString);
            System.out.println(eastString);

        } catch (JessException e) {
            e.printStackTrace();
        }

    }
}
